﻿//********************************************************************************//
//创建日期：<创建日期，2016-09-08>
//创建作者：<孙雪婷，sunxueting@shanghai3h.com>
//功能说明：用户信息维护
//********************************************************************************//

using System;
using System.Data;
using BLL;
using BLL.Access;
using DevExpress.Web;
using Entity;
using Web.Class;

namespace Web.Pages.QuShui.DiXiaS.YongHuGL
{
    public partial class YongHuXXWH : BasePage
    {
        #region 属性

        /// <summary>
        ///     gridView主键(用户信息ID)
        /// </summary>
        private int YONGHUXXID
        {
            get { return Convert.ToInt32(ViewState["_YONGHUXXID"]); }
            set { ViewState["_YONGHUXXID"] = value; }
        }

        /// <summary>
        ///     记录合同号
        /// </summary>
        private string HETONGH
        {
            get { return Convert.ToString(ViewState["HETONGH"]); }
            set { ViewState["HETONGH"] = value; }
        }

        /// <summary>
        ///     保存结果集
        /// </summary>
        private DataTable dtSource
        {
            get { return ViewState["dtSource"] as DataTable; }
            set { ViewState["dtSource"] = value; }
        }

        #endregion

        #region 变量

        /// <summary>
        ///     数据访问操作类
        /// </summary>
        private SpHelper _spHelper = new SpHelper();

        /// <summary>
        ///     用户信息对象
        /// </summary>
        private QS_YG_YONGHUXX _QS_YG_YONGHUXX = new QS_YG_YONGHUXX();

        /// <summary>
        ///     用户信息BLL
        /// </summary>
        private readonly QS_YG_YONGHUXXBLL _QS_YG_YONGHUXXBLL = new QS_YG_YONGHUXXBLL();


        private readonly QS_JX_XINGZHENGQBLL XZQ = new QS_JX_XINGZHENGQBLL();

        private readonly QS_JX_HANGYEFLXXBLL HYFL = new QS_JX_HANGYEFLXXBLL();


        private readonly QS_JX_QUSHUIXXBLL QSYT = new QS_JX_QUSHUIXXBLL();

        private readonly QS_JX_BUMENXXBLL GLBM = new QS_JX_BUMENXXBLL();

        private readonly QS_JX_LIUYUXXBLL LYXX = new QS_JX_LIUYUXXBLL();

        private readonly QS_JX_SHUILIFPXXBLL SLFP = new QS_JX_SHUILIFPXXBLL();

        private readonly QS_JX_SHUIZIYFQBLL SZYFQ = new QS_JX_SHUIZIYFQBLL();


        /// <summary>
        ///     日志BLL
        /// </summary>
        private readonly SYS_LOGBLL _SYS_LOGBLL = new SYS_LOGBLL();

        #endregion

        #region 事件

        /// <summary>
        ///     页面加载事件
        /// </summary>
        protected void Page_Load(object sender, EventArgs e)
        {
            //第一次页面加载
            if (!IsPostBack)
            {
                InitControl();
                InitData();
                Display();
            }
        }


        /// <summary>
        ///     查询
        /// </summary>
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            if (txtConYongHuH.Text == "")
            {
                PageTools.AlertMsg(this, "用户号为空,请输入！");
            }
            else
            {
                Status = GridViewStatus.Init;
                InitControl();
                var dt = _QS_YG_YONGHUXXBLL.SelectQS_YG_YONGHUXXesDynamic(
                    string.Format("I_JLZT=0 and S_YONGHUH={0} ", txtConYongHuH.Text.Trim()), null);
                if (dt == null || dt.Rows.Count < 1)
                {
                    YONGHUXXID = -1;
                    PageTools.AlertMsg(this, "该用户不存在,请重新输入！");
                }
                else
                {
                    YONGHUXXID = Convert.ToInt32(dt.Rows[0]["ID"]);
                    Status = GridViewStatus.Search;
                    Display();
                    SetData();
                }
            }
        }


        /// <summary>
        ///     放弃
        /// </summary>
        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Status = GridViewStatus.Init;
            Display();
            //如果当前有操作项 重新加载数据
            if (YONGHUXXID > 0)
                SetData();
        }

        /// <summary>
        ///     修改
        /// </summary>
        protected void btnEdit_Click(object sender, EventArgs e)
        {
            Status = GridViewStatus.Update;
            //记忆当前修改的ID
            Display();
        }

        /// <summary>
        ///     保存
        /// </summary>
        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (ValidateData())
                SaveData();
        }


        /// <summary>
        ///     响应前台回调
        /// </summary>
        protected void ASPxCallback1_Callback(object source, CallbackEventArgs e)
        {
        }

        #endregion

        #region 方法

        /// <summary>
        ///     初始化控件
        /// </summary>
        private void InitControl()
        {
            txtYONGHUM.Text = "";
            cbmGuanLiBM.Text = "";
            txtShuiJia.Text = "";
            cmbYeWuY.Text = "";
            cmbXingZhengQ.Text = "";
            cmbHuanXianBJ.Text = "";
            cmbQuShuiYT.Text = "";
            cmbHangYeLX.Text = "";
            cmbLiuYu.Text = "";
            cmbShuiLiFP.Text = "";
            cmbShuiZiYFQ.Text = "";
            cmbZhuangBiaoJL.Text = "";
            cmbSFBZ.Text = "";
            txtLianXiR.Text = "";
            txtLianXiBM.Text = "";
            txtYouBian.Text = "";
            txtLianXiDH.Text = "";
            txtDiZhi.Text = "";
            txtCaiWuHM.Text = "";
            cmbSFQY.Text = "";
            txtCaiWuDZ.Text = "";
            txtCaiWuYB.Text = "";
            txtKaiHuHM.Text = "";
            txtKaiHuZH.Text = "";
            txtKaiHuYH.Text = "";
            txtHeTongH.Text = "";
            txtBeiZhu.Text = "";
        }

        /// <summary>
        ///     初始化数据
        /// </summary>
        private void InitData()
        {
            var wtype = BusinessTool.WType;
           // cmbSFBZ.ClientVisible = wtype == 1 ? true : false;
            // sfbz.Visible = wtype == 1 ? true : false;
            if (wtype == 1)
            {
                cmbSFBZ.Items.Add("从电量", "03");
            }
           

            //加载管理部门
            LoadGLBM();
            //加载行政区
            LoadXZQ();
            //加载业务员
            LoadYWY();
            //加载环线标记
            LoadHXBJ();
            //加载取水用途
            LoadQSYT();
            //加载行业分类
            LoadHYFL();
            //加载流域
            LoadLY();
            //水利分片
            LoadSLFP();
            //水资源分区
            LoadSZYFQ();
            var yhh = Request.QueryString["yhh"];
            if (yhh != "" && yhh != null)
            {
                txtConYongHuH.Text = yhh;
                btnSearch_Click(null, null);
            }
        }

        /// <summary>
        ///     加载管理部门
        /// </summary>
        private void LoadGLBM()
        {
            var dt = GLBM.SelectQS_JX_BUMENXXesDynamic("I_JLZT=0 and S_BUMENBH NOT IN(0,600)", null);
            for (var i = 0; i < dt.Rows.Count; i++)
                cbmGuanLiBM.Items.Add(dt.Rows[i]["S_BUMENMC"].ToString(), dt.Rows[i]["S_BUMENBH"]);
            cbmGuanLiBM.Items.Insert(0, new ListEditItem("", "0"));
        }

        /// <summary>
        ///     加载行政区
        /// </summary>
        private void LoadXZQ()
        {
            var dt = XZQ.SelectQS_JX_XINGZHENGQsDynamic("I_JLZT=0", null);
            for (var i = 0; i < dt.Rows.Count; i++)
                cmbXingZhengQ.Items.Add(dt.Rows[i]["S_XINGZHENGQM"].ToString(), dt.Rows[i]["S_XINGZHENGQH"]);
        }

        /// <summary>
        ///     加载业务员
        /// </summary>
        private void LoadYWY()
        {
            PageTools.QSLoadCombox(cmbYeWuY, PageTools.eLoadType.业务员QS, null, "");
        }


        /// <summary>
        ///     加载环线标记
        /// </summary>
        private void LoadHXBJ()
        {
            cmbHuanXianBJ.Items.Add("外环线外", 2);
            cmbHuanXianBJ.Items.Add("外环线内", 1);
        }


        /// <summary>
        ///     加载 取水用途
        /// </summary>
        private void LoadQSYT()
        {
            var dt = QSYT.SelectQS_JX_QUSHUIXXesDynamic("I_JLZT=0", null);
            for (var i = 0; i < dt.Rows.Count; i++)
                cmbQuShuiYT.Items.Add(dt.Rows[i]["S_QUSHUIMC"].ToString(), dt.Rows[i]["S_QUSHUIBH"]);
        }

        /// <summary>
        ///     加载行业分类
        /// </summary>
        private void LoadHYFL()
        {
            var dt = HYFL.SelectQS_JX_HANGYEFLXXesDynamic("I_JLZT=0", null);
            for (var i = 0; i < dt.Rows.Count; i++)
                cmbHangYeLX.Items.Add(dt.Rows[i]["S_FENLEIMC"].ToString(), dt.Rows[i]["S_FENLEIBH"]);
        }

        /// <summary>
        ///     加载流域
        /// </summary>
        private void LoadLY()
        {
            var dt = LYXX.SelectQS_JX_LIUYUXXesDynamic("I_JLZT=0", null);
            for (var i = 0; i < dt.Rows.Count; i++)
                cmbLiuYu.Items.Add(dt.Rows[i]["S_LIUYUMC"].ToString(), dt.Rows[i]["S_LIUYUBH"]);
        }

        /// <summary>
        ///     加载水利分片
        /// </summary>
        private void LoadSLFP()
        {
            var dt = SLFP.SelectQS_JX_SHUILIFPXXesDynamic("I_JLZT=0", null);
            for (var i = 0; i < dt.Rows.Count; i++)
                cmbShuiLiFP.Items.Add(dt.Rows[i]["S_SHUILIFPMC"].ToString(), dt.Rows[i]["S_SHUILIFPBH"]);
        }

        /// <summary>
        ///     加载水资源分区
        /// </summary>
        private void LoadSZYFQ()
        {
            var dt = SZYFQ.SelectQS_JX_SHUIZIYFQsDynamic("I_JLZT=0", null);
            for (var i = 0; i < dt.Rows.Count; i++)
                cmbShuiZiYFQ.Items.Add(dt.Rows[i]["S_SHUIZIYMC"].ToString(), dt.Rows[i]["S_SHUIZIYBH"]);
        }


        /// <summary>
        ///     数据加载
        /// </summary>
        private void LoadData()
        {
        }

        /// <summary>
        ///     用于控制添加、修改、查看三种状态的显示
        /// </summary>
        private void Display()
        {
            switch (Status)
            {
                case GridViewStatus.Init:
                    btnEdit.Visible = true;
                    btnEdit.Enabled = false;
                    btnSave.Visible = false;
                    btnCancel.Visible = false;
                    txtConYongHuH.ClientEnabled = true;
                    txtConYongHuH.Focus();
                    InitControl();
                    SetText(false);
                    break;
                case GridViewStatus.Search:
                    btnEdit.Visible = true;
                    btnEdit.Enabled = true;
                    btnSave.Visible = false;
                    btnCancel.Visible = false;
                    SetText(false);
                    break;
                case GridViewStatus.Update:
                    btnEdit.Visible = false;
                    btnSave.Visible = true;
                    btnCancel.Visible = true;
                    txtConYongHuH.ClientEnabled = false;
                    SetText(true);
                    break;
                case GridViewStatus.Add:
                    btnEdit.Visible = false;
                    btnSave.Visible = true;
                    btnCancel.Visible = true;
                    SetText(true);
                    break;
            }
        }


        /// <summary>
        ///     设置输入框状态
        /// </summary>
        /// <param name="b"></param>
        private void SetText(bool b)
        {
            txtYONGHUM.ClientEnabled = b;
            cbmGuanLiBM.ClientEnabled = b;
            txtShuiJia.ClientEnabled = b;
            cmbYeWuY.ClientEnabled = b;
            cmbXingZhengQ.ClientEnabled = b;
            cmbHuanXianBJ.ClientEnabled = b;
            cmbQuShuiYT.ClientEnabled = b;
            cmbHangYeLX.ClientEnabled = b;
            cmbLiuYu.ClientEnabled = b;
            cmbShuiLiFP.ClientEnabled = b;
            cmbShuiZiYFQ.ClientEnabled = b;
            cmbZhuangBiaoJL.ClientEnabled = b;
            cmbSFBZ.ClientEnabled = b;
            txtLianXiR.ClientEnabled = b;
            txtLianXiBM.ClientEnabled = b;
            txtYouBian.ClientEnabled = b;
            txtLianXiDH.ClientEnabled = b;
            txtDiZhi.ClientEnabled = b;
            txtCaiWuHM.ClientEnabled = b;
            cmbSFQY.ClientEnabled = BusinessTool.WType == 2 ? b : false;
            txtCaiWuDZ.ClientEnabled = b;
            txtCaiWuYB.ClientEnabled = b;
            txtKaiHuHM.ClientEnabled = false;
            txtKaiHuZH.ClientEnabled = false;
            txtKaiHuYH.ClientEnabled = false;
            txtHeTongH.ClientEnabled = false;
            txtBeiZhu.ClientEnabled = b;
        }

        /// <summary>
        ///     保存数据
        /// </summary>
        private void SaveData()
        {
            OperateState operateState;
            //修改操作
            operateState = OperateState.Modify;
            SpHelper _spHelper = new SpHelper();
            int? resmsg = 0;
            _spHelper.P_QSYH_YZXX(BusinessTool.WType, 1, this.txtConYongHuH.Text, out resmsg);
            if (resmsg == 0)
            {
                _spHelper.P_QS_INSERT_YONGHU_HIS(this.txtConYongHuH.Text, LoginInfo.CurrentUser.UserID.ToString(), BusinessTool.WType.ToString());
            }
            //if (helper.P_QS_INSERT_YONGHU_HIS(YONGHUXXID, LoginInfo.CurrentUser.UserID.ToString(),
            //  type,null))
            //{
            //记录ID 只是用于记录修改日志
            _QS_YG_YONGHUXX = _QS_YG_YONGHUXXBLL.SelectQS_YG_YONGHUXX(YONGHUXXID);
            _QS_YG_YONGHUXX.ID = YONGHUXXID;
            _QS_YG_YONGHUXX.S_YONGHUM = txtYONGHUM.Text;


            _QS_YG_YONGHUXX.S_BUMENBH = Convert.ToString(cbmGuanLiBM.Value); //
            _QS_YG_YONGHUXX.N_SHUIJIA = Convert.ToDecimal(txtShuiJia.Text);
            _QS_YG_YONGHUXX.S_YEWUY = cmbYeWuY.Text != "" ? cmbYeWuY.Value.ToString() : "";
            _QS_YG_YONGHUXX.S_XINGZHENGQBH = cmbXingZhengQ.Text != "" ? cmbXingZhengQ.Value.ToString() : "";
            _QS_YG_YONGHUXX.S_HUANXIANBJ = cmbHuanXianBJ.Text != "" ? cmbHuanXianBJ.Value.ToString() : "";
            _QS_YG_YONGHUXX.S_QUSHUIYTBH = cmbQuShuiYT.Text != "" ? cmbQuShuiYT.Value.ToString() : "";
            _QS_YG_YONGHUXX.S_HANGYEBH = cmbHangYeLX.Text != "" ? cmbHangYeLX.Value.ToString() : "";
            _QS_YG_YONGHUXX.S_LIUYUBH = cmbLiuYu.Text != "" ? cmbLiuYu.Value.ToString() : "";
            _QS_YG_YONGHUXX.S_FENPIANBH = cmbShuiLiFP.Text != "" ? cmbShuiLiFP.Value.ToString() : "";
            _QS_YG_YONGHUXX.S_SHUIZIYBH = cmbShuiZiYFQ.Text != "" ? cmbShuiZiYFQ.Value.ToString() : "";
            _QS_YG_YONGHUXX.S_ZHUANGBIAOJL = cmbZhuangBiaoJL.Text != "" ? cmbZhuangBiaoJL.Text : "";
            _QS_YG_YONGHUXX.S_FEETYPE = cmbSFBZ.Text != "" ? cmbSFBZ.Value.ToString() : "";
            _QS_YG_YONGHUXX.S_LIANXIR = txtLianXiR.Text;
            _QS_YG_YONGHUXX.S_LIANXIBM = txtLianXiBM.Text;
            _QS_YG_YONGHUXX.S_YOUBIAN = txtYouBian.Text;
            _QS_YG_YONGHUXX.S_DIANHUA = txtLianXiDH.Text;
            _QS_YG_YONGHUXX.S_DIZHI = txtDiZhi.Text;
            _QS_YG_YONGHUXX.S_CAIWUHM = txtCaiWuHM.Text;
            _QS_YG_YONGHUXX.I_BY1 = BusinessTool.WType == 2 ? Convert.ToInt32(cmbSFQY.Value) : 0;
            _QS_YG_YONGHUXX.S_CAIWUDZ = txtCaiWuDZ.Text;
            _QS_YG_YONGHUXX.S_CAIWUYB = txtCaiWuYB.Text;
            _QS_YG_YONGHUXX.S_KAIHUHM = txtKaiHuHM.Text;
            _QS_YG_YONGHUXX.S_KAIHUH = txtKaiHuZH.Text;
            _QS_YG_YONGHUXX.S_KAIHUYH = txtKaiHuYH.Text;
            _QS_YG_YONGHUXX.S_HETONGH = txtHeTongH.Text;
            _QS_YG_YONGHUXX.S_ZHUSHI = txtBeiZhu.Text;
            _QS_YG_YONGHUXX.S_CAOZUOR = LoginInfo.CurrentUser.UserID.ToString();
            _QS_YG_YONGHUXX.D_CAOZUORQ = DateTime.Now;
            _SYS_LOGBLL.Log(LoginInfo.CurrentUser, BusinessTool.WType.ToString(), "用户管理-用户信息维护", operateState,
                _QS_YG_YONGHUXX, null);
            var result = _QS_YG_YONGHUXXBLL.InsertOrUpdate(_QS_YG_YONGHUXX);
            if (result)
            {
                PageTools.AlertMsg(this, "保存成功！");
                //this.Status = GridViewStatus.Search;
                Status = GridViewStatus.Init;
                Display();
            }
            else
            {
                PageTools.AlertMsg(this, "保存失败！");
            }
            /*}
            else
            {
                PageTools.AlertMsg(this, "保存成功！");
                //this.Status = GridViewStatus.Search;
                Status = GridViewStatus.Init;
                Display();
            }*/

        }

        /// <summary>
        ///     给输入框赋值
        /// </summary>
        private void SetData()
        {
            _QS_YG_YONGHUXX = _QS_YG_YONGHUXXBLL.SelectQS_YG_YONGHUXX(YONGHUXXID);
            txtYONGHUM.Text = _QS_YG_YONGHUXX.S_YONGHUM;
            cbmGuanLiBM.Value = _QS_YG_YONGHUXX.S_BUMENBH; //
            txtShuiJia.Text = Convert.ToDecimal(_QS_YG_YONGHUXX.N_SHUIJIA).ToString("0.00");
            cmbYeWuY.Value = _QS_YG_YONGHUXX.S_YEWUY;
            cmbXingZhengQ.Value = _QS_YG_YONGHUXX.S_XINGZHENGQBH;
            cmbHuanXianBJ.Value = _QS_YG_YONGHUXX.S_HUANXIANBJ;
            cmbQuShuiYT.Value = _QS_YG_YONGHUXX.S_QUSHUIYTBH;
            cmbHangYeLX.Value = _QS_YG_YONGHUXX.S_HANGYEBH;
            cmbLiuYu.Value = _QS_YG_YONGHUXX.S_LIUYUBH;
            cmbShuiLiFP.Value = _QS_YG_YONGHUXX.S_FENPIANBH;
            cmbShuiZiYFQ.Value = _QS_YG_YONGHUXX.S_SHUIZIYBH;
            cmbZhuangBiaoJL.Text = _QS_YG_YONGHUXX.S_ZHUANGBIAOJL;
            cmbSFBZ.Value = _QS_YG_YONGHUXX.S_FEETYPE;
            txtLianXiR.Text = _QS_YG_YONGHUXX.S_LIANXIR;
            txtLianXiBM.Text = _QS_YG_YONGHUXX.S_LIANXIBM;
            txtYouBian.Text = _QS_YG_YONGHUXX.S_YOUBIAN;
            txtLianXiDH.Text = _QS_YG_YONGHUXX.S_DIANHUA;
            txtDiZhi.Text = _QS_YG_YONGHUXX.S_DIZHI;
            txtCaiWuHM.Text = _QS_YG_YONGHUXX.S_CAIWUHM;
            cmbSFQY.Value = BusinessTool.WType == 2 ? _QS_YG_YONGHUXX.I_BY1.ToString() : null ;
            txtCaiWuDZ.Text = _QS_YG_YONGHUXX.S_CAIWUDZ;
            txtCaiWuYB.Text = _QS_YG_YONGHUXX.S_CAIWUYB;
            txtKaiHuHM.Text = _QS_YG_YONGHUXX.S_KAIHUHM;
            txtKaiHuZH.Text = _QS_YG_YONGHUXX.S_KAIHUH;
            txtKaiHuYH.Text = _QS_YG_YONGHUXX.S_KAIHUYH;
            txtHeTongH.Text = _QS_YG_YONGHUXX.S_HETONGH;
            txtBeiZhu.Text = _QS_YG_YONGHUXX.S_ZHUSHI;
        }

        /// <summary>
        ///     保存验证
        /// </summary>
        /// <returns></returns>
        private bool ValidateData()
        {
            if (txtYONGHUM.Text == "")
            {
                PageTools.AlertMsg(this, "用户名不能为空！");
                txtYONGHUM.Focus();
                return false;
            }
            if (txtDiZhi.Text == "")
            {
                PageTools.AlertMsg(this, "地址不能为空！");
                txtDiZhi.Focus();
                return false;
            }
            if (txtYouBian.Text == "")
            {
                PageTools.AlertMsg(this, "邮编不能为空！");
                txtYouBian.Focus();
                return false;
            }
            if (txtShuiJia.Text == "")
            {
                PageTools.AlertMsg(this, "水价不能为空！");
                txtShuiJia.Focus();
                return false;
            }
            return true;
        }

        #endregion
    }
}